package com.dotstone.chipism.adapter;

import java.util.List;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.DeviceType;
import com.dotstone.chipism.util.DeviceManager;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;

public class SearchResultAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<Device> data;
	private int type;
	private Context context;

//	public SearchResultAdapter(Context context, List<Device> data, int type) {
//		inflater = LayoutInflater.from(context);
//		this.context = context;
//		this.data = data;
//		this.type = type;
//	}
	
	public SearchResultAdapter(Context context, List<Device> data) {
		inflater = LayoutInflater.from(context);
		this.context = context;
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Device getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setData(List<Device> data) {
		this.data = data;
	}

	public List<Device> getData() {
		for (int i = 0; i < data.size(); i++) {
			boolean e = DeviceManager.getInstance().checkName(
					data.get(i).getDeviceName());
			data.get(i).setSelected(e);
		}
		return data;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		final ViewHolder holder;
//		if (rowView == null) {
			rowView = inflater.inflate(R.layout.item_search_result, null);
			holder = new ViewHolder();
			holder.mDeviceNameTV = (EditText) rowView
					.findViewById(R.id.device_tv);
			// holder.existed = (TextView) rowView.findViewById(R.id.existed);
			holder.mTypeImg = (ImageView) rowView.findViewById(R.id.type_img);
			rowView.setTag(holder);
//		} else {
//			holder = (ViewHolder) rowView.getTag();
//		}
		Device device = data.get(position);
		holder.mDeviceNameTV.setText(device.getDeviceName());
		if (data.get(position).existed) {

		}
		switch (device.getDeviceType()) {
		case DeviceType.DEVICE_TYPE_LOCK:
			holder.mTypeImg.setImageResource(R.drawable.btn103);
			break;
		case DeviceType.DEVICE_TYPE_RGB_LIGHT:
			holder.mTypeImg.setImageResource(R.drawable.btn105);
			break;
		case DeviceType.DEVICE_TYPE_SWITCH:
			holder.mTypeImg.setImageResource(R.drawable.btn106);
			break;
		case DeviceType.DEVICE_TYPE_SOCKET:
			holder.mTypeImg.setImageResource(R.drawable.btn107);
			break;
            case DeviceType.DEVICE_TYPE_CURTAIN:
                holder.mTypeImg.setImageResource(R.drawable.ic_curtain);
                break;
		default:
			break;
		}
		Log.i("wmy", device.getDeviceName()+" "+device.getDeviceAddress()+" "+device.existed);
		if (device.existed) {
			holder.mDeviceNameTV.setTextColor(context.getResources().getColor(
					R.color.red));
		} else {
			holder.mDeviceNameTV.setTextColor(context.getResources().getColor(
					R.color.gray_introdution));
		}
		holder.mDeviceNameTV.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				holder.mDeviceNameTV.requestFocus();
			}
		});
		holder.mDeviceNameTV.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {

			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				holder.mDeviceNameTV.setTextColor(context.getResources()
						.getColor(R.color.gray_introdution));
				data.get(position).setDeviceName(arg0.toString());
			}
		});
//		 }

		return rowView;
	}

	public class ViewHolder {
		public ImageView mTypeImg;
		public EditText mDeviceNameTV;
		// public TextView existed;
	}
}